package com.exam.danangtranel.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import com.exam.danangtranel.filed.Travel;
import com.example.danangtravel.R;
import com.example.danangtravel.R.drawable;
import com.example.danangtravel.allMethod;
import com.example.danangtravel.Travel.FlowConver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TbTravelPointAdapter extends BaseAdapter {
	public Context context;
	public ArrayList<Travel> arrayTravel;
	static InputStream bitmap = null;
	static Travel travel;

	public TbTravelPointAdapter(Context c, ArrayList<Travel> array) {
		this.context = c;
		this.arrayTravel = array;
		
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return arrayTravel.size();
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arrayTravel.get(arg0);
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	public View getView(int arg0, View arg1, ViewGroup arg2) {
		travel = arrayTravel.get(arg0);
		// TODO Auto-generated method stub
		if (arg1 == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			arg1 = inflater.inflate(R.layout.dulich_viewitem, null);
		}
		TextView tv_name =(TextView)arg1.findViewById(R.id.tv_name);
		tv_name.setText("" + travel.getName());
		TextView tv_diachi = (TextView)arg1.findViewById(R.id.tv_address);
		tv_diachi.setText("" + travel.getAddress());
		TextView tv_information = (TextView)arg1.findViewById(R.id.tv_information);
		tv_information.setText("" + travel.getInformation());
		ImageView im_trv = (ImageView)arg1.findViewById(R.id.im_hienthi);
					try {
						im_trv.setImageBitmap(getimg(travel.getImage()));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
//		im_trv.setBackgroundResource(R.drawable.du1);
		im_trv.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context,FlowConver.class);
				context.startActivity(intent);
			}
		});
		return arg1;
	}
		
	public Bitmap getimg(String name) throws IOException {
		bitmap = context.getAssets().open("Travel/" + name);
		Bitmap bit = BitmapFactory.decodeStream(bitmap);
		bitmap.close();
		return bit;
	}

}
